package com.jsyl.lwbk.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class JsonUtil {
	
	private static ObjectMapper objectMapper = new ObjectMapper();
	private static ObjectMapper objectMapperNoAnnotations = new ObjectMapper();

	static{
		initObjectMapper(objectMapper,true);
		initObjectMapper(objectMapperNoAnnotations,false);
	}

	public static void initObjectMapper(ObjectMapper objectMapper,boolean useAnnotations){
		//可以用单引号设置
		objectMapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
		//json属性可以没有设置转义
		objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
		objectMapper.configure(Feature.IGNORE_UNDEFINED, true);

		objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);
		objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
		//json中有属性但在bean中没有   设置成功
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		//使用注解
		objectMapper.configure(MapperFeature.USE_ANNOTATIONS, useAnnotations);
	}

	/**
	 * json 串转对象
	 * @param jsonStr
	 * @param clzz
	 * @param <T>
	 * @return
	 * @throws IOException
	 */
	public static <T> T readJsonObject(String jsonStr, Class<T> clzz) throws IOException{
		return objectMapper.readValue(jsonStr, clzz);
	}

	/**
	 * 对象转json
	 * @param object
	 * @return
	 * @throws JsonProcessingException
	 */
	public static String readObjectToJson(Object object) throws JsonProcessingException{
		String str = objectMapper.writeValueAsString(object);
		return str;
	}

	/**
	 * 对象转对象
	 * @param object
	 * @param tClass
	 * @param <T>
	 * @return
	 * @throws JsonProcessingException
	 */
	public static <T> T convertObjectToJsonObject(Object object,Class<T> tClass) throws IllegalArgumentException{
		return objectMapper.convertValue(object, tClass);
	}

	/**
	 * json 串转对象 不使用注解
	 * @param jsonStr
	 * @param clzz
	 * @param <T>
	 * @return
	 * @throws IOException
	 */
	public static <T> T readJsonObjectNoAnnotations(String jsonStr, Class<T> clzz) throws IOException{
		return objectMapperNoAnnotations.readValue(jsonStr, clzz);
	}

	/**
	 * 对象转json 不使用注解
	 * @param object
	 * @return
	 * @throws JsonProcessingException
	 */
	public static String readObjectToJsonNoAnnotations(Object object) throws JsonProcessingException{
		String str = objectMapperNoAnnotations.writeValueAsString(object);
		return str;
	}

	/**
	 * 替换数组，列表为字符串
	 * @param jsonObject
	 * @return
	 */
	public static JSONObject replaceCollectionToString(JSONObject jsonObject){
		Set<String> set = jsonObject.keySet();
		for(String key :set){
			Object value = jsonObject.get(key);
			if(value == null){
				continue;
			}

			if(value instanceof  Collection){
				Collection collection = (Collection)value;
				JSONArray jsonArray = new JSONArray();
				for(Object o : collection){
					jsonArray.add(o);
				}
				jsonObject.put(key,jsonArray.toJSONString());
				continue;
			}

			if(value instanceof JSONArray){
				JSONArray jsonArray = (JSONArray)value;
				jsonObject.put(key,jsonArray.toJSONString());
				continue;
			}

		}
		return jsonObject;
	}


	/**
	 * json 转map
	 * @param jsonObject
	 * @return
	 */
	public static Map<String,Object> convertJSONObjectToMap(JSONObject jsonObject){
		if(jsonObject == null){
			return null;
		}

		Map<String,Object> map = new HashMap<>();
		Set<String> set = jsonObject.keySet();
		for(String key :set){
			map.put(key,jsonObject.get(key));
		}
		return map;
	}
	
}
